package com.linc.countdown;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.android.widget.demo.R;

import java.math.BigDecimal;

public class CountDownActivity extends AppCompatActivity {
    private final static String TAG = "CountDownActivity";
    private TextView mTvCountdown;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_down);

        mTvCountdown = (TextView)findViewById(R.id.tv_show);
        countdownToStart();
    }

    private void countdownToStart() {
//        new CountDownTimer(5550, 1000) {
//
//            @Override
//            public void onTick(long millisUntilFinished) {
//                double time = millisUntilFinished*0.001;
//                Log.d(TAG, "onTick: " + Math.round(time)+" millisUntilFinished:"
//                        +millisUntilFinished);
//                mTvCountdown.setText(""+ Math.round(time));
//            }
//
//            @Override
//            public void onFinish() {
//                Log.d(TAG,"onFinish");
//                mTvCountdown.setText("");
//
//            }
//        }.start();
//
//        new AccurateCountdownTimer(5000,1000) {
//
//            @Override
//            public void onTick(long millisUntilFinished) {
//                Log.d(TAG, "onTick: " + Math.round(millisUntilFinished / 1000)+" millisUntilFinished:"
//                        +millisUntilFinished);
//                mTvCountdown.setText(""+ Math.round(millisUntilFinished / 1000));
//            }
//
//            @Override
//            public void onFinish() {
//                Log.d(TAG,"onFinish");
//                mTvCountdown.setText("");
//            }
//        }.start();
        new RealCountdownTimer(5005,1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                double time = millisUntilFinished*0.001;
                Log.d(TAG,"the time "+time);
                BigDecimal bigTime = new BigDecimal(time).setScale(0, BigDecimal.ROUND_HALF_UP);
                Log.d(TAG,"bigTime "+bigTime);
                Log.d(TAG, "onTick: " + Math.round(time)+" millisUntilFinished:"
                        +millisUntilFinished);
                mTvCountdown.setText(""+ Math.round(time));
            }

            @Override
            public void onFinish() {
                Log.d(TAG,"onFinish");
                mTvCountdown.setText("");
                printPackageName();
                printPhoneInfo();
            }
        }.start();
    }

    private void printPackageName() {
        Log.d(TAG,"package name: "+getPackageName());
        mTvCountdown.append("\npackage name: "+getPackageName());
        PackageInfo info;
        try {
            info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
            String versionName = info.versionName;
            int versionCode = info.versionCode;
            String packageNames = info.packageName;
            mTvCountdown.append("\nversion name: "+versionName+
                    "\nversion code: "+versionCode+
                    "\npackage name: "+packageNames);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void printPhoneInfo() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        //device id
        Log.d(TAG,"device id: "+telephonyManager.getDeviceId());
        mTvCountdown.append("\ndevice id: " + telephonyManager.getDeviceId());
        //phone number
        mTvCountdown.append("\nphone number: "+telephonyManager.getLine1Number());
        //OperatorName
        mTvCountdown.append("\noperator name: "+telephonyManager.getSimOperatorName());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_count_down, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
